package com.example.tse.rbhttpservice.service.moments;


import android.content.Context;

import com.example.tse.rbhttpservice.service.HttpService;
import com.example.tse.rbhttpservice.service.RemoteServiceListenerT;
import com.example.tse.rbhttpservice.service.ResponseServiceListenerT;
import com.example.tse.rbhttpservice.service.RudderResponse;
import com.example.tse.rbhttpservice.service.moments.model.MomentsModuleGetActivityList;
import com.example.tse.rbhttpservice.service.moments.model.MomentsModuleGetActivityListRequest;
import com.example.tse.rbhttpservice.service.moments.model.MomentsModuleGetActivityListResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.kymjs.kjframe.http.HttpParams;

import java.lang.reflect.Type;
import java.util.List;


/**
 * 
 * @author wliu
 *    edi  tse
 */
public class MomentsModuleServiceImpl implements MomentsModuleService {
	private Context context;
	private static Gson gson = new Gson();
	
	
	public MomentsModuleServiceImpl(Context context) {
		this.context = context;
	}
	/**
	 * 点击首页版块进入版块活动列表，显示该版块下的活动列表
	 * @param request
	 * @param listener
	 * @return MomentsModuleGetActivityListResponse
	 */		
	 
	    public void getActivityList(MomentsModuleGetActivityListRequest request, final RemoteServiceListenerT<MomentsModuleGetActivityListResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("moduleId",request.getModuleId());

        final String url = "moments/module/getActivityList";


        
        /**
        module
        getActivityList
        GetActivityList
        点击首页版块进入版块活动列表，显示该版块下的活动列表
        GET
        MomentsModule
        MomentsModuleGetActivityList
        moments/module/getActivityList
        array
        */
	
           final Type type = new TypeToken<List<MomentsModuleGetActivityList>>() {
        }.getType();



            new HttpService().getList(context, url, params, MomentsModuleGetActivityListResponse.class, new ResponseServiceListenerT<RudderResponse>() {

             @Override
             public void onPreStart() {
                 super.onPreStart();
                 listener.PreStart();
             }

             @Override
             public void ok(RudderResponse rudderResponse) {
                 super.ok(rudderResponse);
                 List<MomentsModuleGetActivityList> items = gson.fromJson(rudderResponse.getBody(), type);
                 MomentsModuleGetActivityListResponse responce = new MomentsModuleGetActivityListResponse();
                 responce.setGetActivityList(items);
                 listener.ok(responce);
             }

             @Override
             public void okData(RudderResponse rudderResponse) {
                 super.okData(rudderResponse);

                 if (rudderResponse == null) {
                     listener.okData(new MomentsModuleGetActivityListResponse());
                 } else {
                     List<MomentsModuleGetActivityList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MomentsModuleGetActivityListResponse responce = new MomentsModuleGetActivityListResponse();
                     responce.setGetActivityList(items);
                     listener.okData(rudderResponse == null ? new MomentsModuleGetActivityListResponse() : responce);
                 }
             }

             @Override
             public void okData(RudderResponse rudderResponse, int fromWhere) {
                 super.okData(rudderResponse, fromWhere);


                 if (rudderResponse == null) {
                     listener.okData(new MomentsModuleGetActivityListResponse(), fromWhere);
                 } else {
                     List<MomentsModuleGetActivityList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MomentsModuleGetActivityListResponse responce = new MomentsModuleGetActivityListResponse();
                     responce.setGetActivityList(items);
                     listener.okData(rudderResponse == null ? new MomentsModuleGetActivityListResponse() : responce, fromWhere);
                 }


             }

             @Override
             public void onFailure(int errorNo, String strMsg) {
                 super.onFailure(errorNo, strMsg);
                 listener.failue(errorNo, strMsg);
             }

             @Override
             public void onFinish() {
                 super.onFinish();
                 listener.Finish();
             }
         });
    }            
        
        

   
	
	
}